﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class pay : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            initTablePriceOverview();
        }

        private void initTablePriceOverview()
        {
            DateTime dateTime = DateTime.Parse(Request.QueryString["datetime"]);
            int roomNumber = Convert.ToInt32(Request.QueryString["roomnumber"]);
            String movieName = Request.QueryString["moviename"];
            int adults = Convert.ToInt32(Request.QueryString["adultcounter"]);
            int children = Convert.ToInt32(Request.QueryString["childcounter"]);
            int fiftyPlus = Convert.ToInt32(Request.QueryString["50pluscounter"]);
            int cjpStudents = Convert.ToInt32(Request.QueryString["cjpstudentcounter"]);
            int free = Convert.ToInt32(Request.QueryString["freecounter"]);
            int drieDGlasses = Convert.ToInt32(Request.QueryString["3dglassescounter"]);

            labelMoviename.Text = movieName;
            labelDatetime.Text = dateTime.ToString();
            labelRoom.Text = roomNumber.ToString();
            labelAdultCount.Text = adults.ToString();
            labelChildCount.Text = children.ToString();
            labelFiftyPlusCount.Text = fiftyPlus.ToString();
            labelCJPStudentCount.Text = cjpStudents.ToString();
            labelFreeCount.Text = free.ToString();
            labelDrieDGlassesCount.Text = drieDGlasses.ToString();

            decimal adultPrice = adults * getPrice("volwassen");
            decimal childPrice = children * getPrice("kind");
            decimal fiftyPlusPrice = fiftyPlus * getPrice("50+");
            decimal cjpStudentPrice = cjpStudents * getPrice("CJP/student");
            decimal freePrice = free * getPrice("gratis");
            decimal drieDGlassesPrice = drieDGlasses * getPrice("3D Bril");

            labelAdultPrice.Text = "EUR " + adultPrice.ToString("#0.#####");
            labelChildPrice.Text = "EUR " + childPrice.ToString("#0.#####");
            labelFiftyPlusPrice.Text = "EUR " + fiftyPlusPrice.ToString("#0.#####");
            labelCJPStudentPrice.Text = "EUR " + cjpStudentPrice.ToString("#0.#####");
            labelFreePrice.Text = "EUR " + freePrice.ToString("#0.#####");
            labelDrieDGlassesPrice.Text = "EUR " + drieDGlassesPrice.ToString("#0.#####");
            labelTotalPrice.Text = "EUR " + (adultPrice + childPrice + fiftyPlusPrice + cjpStudentPrice + freePrice + drieDGlassesPrice).ToString("#0.#####");
        }

        private decimal getPrice(String type)
        {
            var prices = from t in Database.Tickettypes
                         where t.naam.Equals(type)
                         select t;

            return prices.First().prijs;
        }

        private List<Ticket> createTickets()
        {
            DateTime dateTime = DateTime.Parse(Request.QueryString["datetime"]);
            int roomNumber = Convert.ToInt32(Request.QueryString["roomnumber"]);
            String movieName = Request.QueryString["moviename"];
            String dsgdfsg = Request.QueryString["seats"];
            String[] seats = Request.QueryString["seats"].Split(';');
            int adults = Convert.ToInt32(Request.QueryString["adultcounter"]);
            int children = Convert.ToInt32(Request.QueryString["childcounter"]);
            int fiftyPlus = Convert.ToInt32(Request.QueryString["50pluscounter"]);
            int cjpStudents = Convert.ToInt32(Request.QueryString["cjpstudentcounter"]);
            int free = Convert.ToInt32(Request.QueryString["freecounter"]);
            int drieDGlasses = Convert.ToInt32(Request.QueryString["3dglassescounter"]);

            var shows = from s in Database.Voorstellings
                        where s.datumtijd.Equals(dateTime) && s.Zaal.nummer == roomNumber && s.Film.naam.Replace((char)39, ' ').Equals(movieName)
                        select s;

            Voorstelling show = shows.First();
            List<Ticket> tickets = new List<Ticket>();

            int counter = 0;

            int temp = counter;
            for (int i = counter; i < adults + temp; i++)
            {
                tickets.Add(createTicket(show, seats[i], "volwassen"));
                counter++;
            }

            temp = counter;
            for (int i = counter; i < children + temp; i++)
            {
                tickets.Add(createTicket(show, seats[i], "kind"));
                counter++;
            }

            temp = counter;
            for (int i = counter; i < fiftyPlus + temp; i++)
            {
                tickets.Add(createTicket(show, seats[i], "50+"));
                counter++;
            }

            temp = counter;
            for (int i = counter; i < cjpStudents + temp; i++)
            {
                tickets.Add(createTicket(show, seats[i], "CJP/student"));
                counter++;
            }

            temp = counter;
            for (int i = counter; i < free + temp; i++)
            {
                tickets.Add(createTicket(show, seats[i], "gratis"));
                counter++;
            }

            Database.SubmitChanges();

            return tickets;
        }

        private Ticket createTicket(Voorstelling show, String seat, String ticketType)
        {
            String[] temp = seat.Split(',');
            int rowNumber = Convert.ToInt32(temp[0]);
            int seatNumber = Convert.ToInt32(temp[1]);

            Ticket ticket = new Ticket { stoel_nummer = seatNumber, rij_nummer = rowNumber, zaal_nummer = show.Zaal.nummer, voorstelling_datumtijd = show.datumtijd, film_naam = show.Film.naam, tickettype_naam = ticketType, status_naam = "betaald" };
            Database.Tickets.InsertOnSubmit(ticket);

            return ticket;
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            List<Ticket> tickets = createTickets();

            String queryString = "printtickets.aspx?moviename=" + tickets.First().Voorstelling.Film.naam + "&tickets=" + tickets.First().nummer + ":" + tickets.First().Tickettype.naam + ":" + tickets.First().Stoel.nummer + ":" + tickets.First().Stoel.Rij.nummer;

            for (int i = 1; i < tickets.Count; i++)
            {
                Ticket t = tickets[i];
                queryString += ";" + t.nummer + ":" + t.Tickettype.naam + ":" + t.Stoel.nummer + ":" + t.Stoel.Rij.nummer;
            }

            Response.Redirect(queryString);
        }
    }
}